package com.mop.springcloud.corebase.config;

import com.mop.springcloud.corebase.DefaultMopApplicationInitializer;
import com.mop.springcloud.corebase.MopApplicationInitializer;
import com.mop.springcloud.corebase.controller.MopBuildInController;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.DefaultPropertiesFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;

import java.util.List;

/**
 * Class Name: EnableMopAutoConfigurationImport
 * Create Date: 18-3-15 下午7:41
 * Creator: Chim·Zigui
 * Version: v1.0
 * Updater:
 * Date Time:
 * Description:
 */
@Configuration
public class EnableMopAutoConfigurationImport implements DeferredImportSelector,
        BeanClassLoaderAware {

    private ClassLoader classLoader;

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        MopApplicationInitializer.setMopApplicationInitializerEnable(true);
        this.classLoader = classLoader;
    }

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        List<String> beanNames = SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, classLoader);

        // Import Default PropertiesFactory
        beanNames.add(DefaultPropertiesFactory.class.getName());
        // Import Default MopApplicationInitializer
        beanNames.add(DefaultMopApplicationInitializer.class.getName());
        // Import Build In Controller
        beanNames.add(MopBuildInController.class.getName());

        return beanNames.toArray(new String[beanNames.size()]);
    }

}
